Skip to content

Make LDF <: AbstractModel #937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged

Make LDF <: AbstractModel #937

merged 1 commit into from
May 25, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented May 25, 2025

See TuringLang/Turing.jl#2555 -- this is one of the necessary steps, or else you'll get errors like

julia> sample(ldf, NUTS(), 100)
ERROR: MethodError: no method matching mcmcsample(::Random.TaskLocalRNG, ::LogDensityFunction{…}, ::DynamicPPL.Sampler{…}, ::Int64; chain_type::UnionAll, progress::Bool)
The function `mcmcsample` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  mcmcsample(::Random.AbstractRNG, ::AbstractMCMC.AbstractModel, ::AbstractMCMC.AbstractSampler, ::Integer; progress, progressname, callback, num_warmup, discard_initial, thinning, chain_type, initial_state, kwargs...)

This really shouldn't change anything in DynamicPPL.

Copy link
Contributor

github-actions bot commented May 25, 2025

Benchmark Report for Commit a22771d

Computer Information

Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                  9.4 |                 1.6 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                723.4 |                33.8 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                357.6 |                53.7 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1188.1 |                28.1 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               3272.7 |                24.1 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1441.6 |                29.3 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |                956.2 |                 5.2 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5381.6 |                 4.1 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |                993.6 |                 9.0 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              60707.9 |                 3.6 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               8526.6 |                10.0 |
|               Dynamic |        10 |    mooncake |             typed |   true |                143.6 |                11.6 |
|              Submodel |         1 |    mooncake |             typed |   true |                 13.0 |                 6.5 |
|                   LDA |        12 | reversediff |             typed |   true |                445.5 |                 5.6 |

Copy link

codecov bot commented May 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.91%. Comparing base (e350cd3) to head (a22771d).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #937   +/-   ##
=======================================
  Coverage   82.91%   82.91%           
=======================================
  Files          36       36           
  Lines        3962     3962           
=======================================
  Hits         3285     3285           
  Misses        677      677           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coveralls
Copy link

coveralls commented May 25, 2025

Pull Request Test Coverage Report for Build 15240168224

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 58 unchanged lines in 5 files lost coverage.
  • Overall coverage remained the same at 83.08%

Files with Coverage Reduction New Missed Lines %
src/experimental.jl 3 0.0%
src/varinfo.jl 4 84.14%
src/logdensityfunction.jl 14 50.0%
ext/DynamicPPLJETExt.jl 16 0.0%
src/test_utils/ad.jl 21 0.0%
Totals Coverage Status
Change from base Build 15213861039: 0.0%
Covered Lines: 3285
Relevant Lines: 3954

💛 - Coveralls

@penelopeysm penelopeysm enabled auto-merge May 25, 2025 17:35
@penelopeysm penelopeysm added this pull request to the merge queue May 25, 2025
@penelopeysm penelopeysm removed this pull request from the merge queue due to a manual request May 25, 2025
@penelopeysm penelopeysm merged commit a8a7026 into main May 25, 2025
21 of 22 checks passed
@penelopeysm penelopeysm deleted the py/ldf-abstractmodel branch May 25, 2025 17:38
@sunxd3
Copy link
Member

sunxd3 commented May 30, 2025

I had some issue in JuliaBUGS some time ago: if I mark BUGSModel as a subtype of AbstactModel, it will hit https://github.com/TuringLang/AbstractMCMC.jl/blob/ececa17d5c354aa3279e7ce970ac768751c22fcf/src/sample.jl#L96-L106

and if I don't it will hit https://github.com/TuringLang/AbstractMCMC.jl/blob/ececa17d5c354aa3279e7ce970ac768751c22fcf/src/logdensityproblems.jl#L94-L108

because the former type is more specific.

Somehow here feels like the opposite. A bit confused.

@penelopeysm
Copy link
Member Author

penelopeysm commented May 30, 2025

The dispatch chain can be very confusing --- in the case of Turing samplers, they only have step() methods defined for DynamicPPL.Model, so regardless of whether LDF subtypes AbstractModel it will error in the end anyway. (There are quite a few methods in Turing which claim to work on AbstractModel but don't actually work with anything except DynamicPPL.Model)

I think the main issue with going down the second path (via LogDensityModel) is that that becomes an opaque wrapper around the LDF and I think the samplers tend to need more information about what's inside the LDF. For example HMCState carries a varinfo around with it https://github.com/TuringLang/Turing.jl/blob/1a7062765ceb6942a4d4eae609ece401ff13d0a6/src/mcmc/hmc.jl#L5-L18 which doesn't make sense unless you know you're sampling from DynamicPPL.Model. externalsampler helps to bridge this gap a bit (by moving the varinfo to the external bit) but Gibbs in particular is very awkward to deal with

@sunxd3
Copy link
Member

sunxd3 commented May 30, 2025

Make sense, thanks Penny!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants